package cn.iocoder.yudao.module.handguard.service.hometwo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.hometwo.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.hometwo.HomeTwoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.hometwo.HomeTwoConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.hometwo.HomeTwoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 首页成交榜-列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HomeTwoServiceImpl implements HomeTwoService {

    @Resource
    private HomeTwoMapper homeTwoMapper;

    @Override
    public Long createHomeTwo(HomeTwoCreateReqVO createReqVO) {
        // 插入
        HomeTwoDO homeTwo = HomeTwoConvert.INSTANCE.convert(createReqVO);
        homeTwoMapper.insert(homeTwo);
        // 返回
        return homeTwo.getId();
    }

    @Override
    public void updateHomeTwo(HomeTwoUpdateReqVO updateReqVO) {
        // 校验存在
        validateHomeTwoExists(updateReqVO.getId());
        // 更新
        HomeTwoDO updateObj = HomeTwoConvert.INSTANCE.convert(updateReqVO);
        homeTwoMapper.updateById(updateObj);
    }

    @Override
    public void deleteHomeTwo(Long id) {
        // 校验存在
        validateHomeTwoExists(id);
        // 删除
        homeTwoMapper.deleteById(id);
    }

    private void validateHomeTwoExists(Long id) {
        if (homeTwoMapper.selectById(id) == null) {
            throw exception(HOME_TWO_NOT_EXISTS);
        }
    }

    @Override
    public HomeTwoDO getHomeTwo(Long id) {
        return homeTwoMapper.selectById(id);
    }

    @Override
    public List<HomeTwoDO> getHomeTwoList() {
        LambdaQueryWrapper<HomeTwoDO> wrapper =new LambdaQueryWrapper<>();
        wrapper.orderByDesc(HomeTwoDO::getCreateTime);
        List<HomeTwoDO> homeTwoDOList = homeTwoMapper.selectList(wrapper);
        return homeTwoDOList;
    }

    @Override
    public PageResult<HomeTwoDO> getHomeTwoPage(HomeTwoPageReqVO pageReqVO) {
        return homeTwoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HomeTwoDO> getHomeTwoList(HomeTwoExportReqVO exportReqVO) {
        return homeTwoMapper.selectList(exportReqVO);
    }

}
